import { observable, action, toJS } from 'mobx';
import { WeaTableNew, WeaForm, WeaLogView } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import * as API from '../apis/searchSet.js';
import weesoStore from './weeso';
const {TableStore} = WeaTableNew;
const {LogStore} = WeaLogView;
const getLabel = WeaLocaleProvider.getLabel;
class WeesonSeachSetStore {
  @observable dialogVisible = false;
  @observable customizationDialogVisible = false;
  @observable searchTableStore = new TableStore();
  @observable commonConditionTableStore = new TableStore();
  @observable form = new WeaForm(); 
  @observable conditions = [];   
  @observable stepForm = new WeaForm(); 
  @observable stepConditions = []; 
  @observable addCommonForm = new WeaForm(); 
  @observable addCommonConditions = []; 
  @observable currentStep = 0; 
  @observable stepContents = [
    { step: 0, label: getLabel('524084','页面基本设置') },
    { step: 1, label: getLabel('524085','常用条件设置') },
    { step: 2, label: getLabel('524261','搜索结果页设置') },
    { step: 3, label: getLabel('555','完成') }
  ]; 

  @observable collectParams = {
    favname:getLabel('524262','微搜设置'),
    favouritetype: 1,
    objid:0, 
    link : 'wui/engine.html#/meetingengine/msgdefine',
    importantlevel:1,
  };

  @observable addCommonVisible = false;
  @observable maintenanceKeyAddCommonVisible = false;
  @observable page_uuid = 'FULLSEARCH';
  @observable recordId = '';
  @observable recordParams = {};
  @observable addRecordId = '';
  @observable urlDialogVisible = false;
  @observable homeLoading = false;
  @observable search_type = '';
  @observable page_descStr = '';
  @observable baseDatas = {};
  @observable commonConditionParams = {};
  @observable browserKeyArr = []; 
  @observable commonPageType = '';
  @observable hasPageIcon = true;
  @observable hasPageResultLogo= true;
  @observable pageCusCurrentId = '';
  @observable adSearchForm = new WeaForm();
  @observable adSearchFormConditions = [];
  @observable commonDialogVisible = false;
  @observable commonDialogInputVal = '';
  @observable searchTagParams = {};
  @observable logStore = new LogStore();
  @observable logvisible = false;
  @observable logSmallType = '3';
  @observable logType = '35';
  @observable topTabsVisible = true;
  @observable homeToResultPage = undefined;
  @observable editCommonConditionRetdatas = {};
  @observable searchPageRight = true;
  @observable searchDefinePageInputVal = '';
  @observable searchDefineCommonInputVal = '';
  @observable recordType = '';
  
  @action
  setState = (params = {}, callback) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });

    typeof callback === 'function' && callback();
  }

  getSeachTable = (params) => {
    API.getSeachTable(params).then(result => {
      // this.searchTableStore.getDatas(result.sessionkey, 1);
    })
  }

  getSeachCustomTable = (params) => {
    this.form = new WeaForm();
    this.conditions = [];
    API.getSeachCustomTable(params).then(result => {
      if(result.api_status) {
        this.form.initFormFields(result.fields);
        this.conditions = result.fields;

      }
    });
  }

  getCustompageBaseDatas = (params, type) => {
    // cuspage_type: 1:基础信息  2: 仅获取历史搜索记录 （刷新的时候） 3: 仅获取常用条件（刷新的时候）  其他值: 全部获取
    if(type !== 'init') {
      params = {
        page_uuid: this.page_uuid,
        ...params
      };
    }

    this.homeLoading = true;
    API.getCustompageBaseDatas(params).then(result => {
      this.homeLoading = false;
      if(type === 'init') {
        this.page_uuid = result.page_uuid;
      }
      if(result.api_status) {
        if(result.cuspage_type === '1') {
          this.baseDatas = result;
          weesoStore.setState({ defalutMoreKey: result.search_type });
        }
        if(result.cuspage_type === '2') {
          weesoStore.setState({ historySearchList: result.historySearchList });
        }
        if(result.cuspage_type === '3') {
          weesoStore.setState({ tagList: result.tagList });
        }
        if(result.search_type) { 
          this.search_type = result.search_type;
          if(result.search_type === 'ALL') {
            this.topTabsVisible = true;
          } else {
            this.topTabsVisible = false;
          }
        }
        if(result.page_descStr) { 
          this.page_descStr = result.page_descStr;
        }
      }
    });
  }

  //新建编辑保存 常用条件
  doSaveSearchTag = (params, callback) => {  
    params = {
      page_uuid: this.page_uuid,
      search_type: weesoStore.defalutMoreKey,
      ...params
    };
    API.doSaveSearchTag(params).then(result => {
      if(result.api_status) {
        typeof callback === 'function' && callback(result);
      }
    });
  }

  //插入历史搜索记录
  doSaveHistorySearch = (params) => { 
    params = {
      page_uuid: this.page_uuid,
      search_type: weesoStore.defalutMoreKey,
      ...params
    };
    API.doSaveHistorySearch(params).then(result => {
    });
  } 

  //删除常用条件
  delSearchTag = (params, callback) => { 
    params = {
      page_uuid: this.page_uuid,
      ...params
    };
    API.delSearchTag(params).then(result => {
      if(result.api_status) {
        typeof callback === 'function' && callback();
      }
    });
  }

  //删除历史搜索条件
  delHistorySearch = (params, callback) => { 
    params = {
      page_uuid: this.page_uuid,
      ...params
    };
    API.delHistorySearch(params).then(result => {
      if(result.api_status) {
        typeof callback === 'function' && callback();
      }
    });
  }

  getCusPageSetDtl = (params, callback) => { 
    params = {
      page_uuid: this.page_uuid,
      ...params,
    };
    // params.id = 33
    // params.type = 3
    this.stepForm = new WeaForm();
    this.stepConditions = [];
    API.getCusPageSetDtl(params).then(result => {
      if(result.api_status) {
        this.page_icon_dir = result.page_icon_dir;
        if(result.fields === undefined) {
          this.stepForm = new WeaForm();
          this.stepConditions = [];
        } else {
          this.stepForm.initFormFields(result.fields);
          this.stepForm.updateFields()
          this.stepConditions = result.fields;
        }
        typeof callback === 'function' && callback(result);
      }
    });
  }

  getCusPageSetList = (params, callback) => { 
    params = {
      page_uuid: this.page_uuid,
      page_title: this.searchDefinePageInputVal,
      ...params,
    };
    API.getCusPageSetList(params).then(result => {+ ``
      if(result.api_status) {
        if(result.status === 'noright') {
          this.searchPageRight = false;
        } else {
          this.searchPageRight = true;
          this.searchTableStore.getDatas(result.sessionkey, 1);
          typeof callback === 'function' && callback();
        }
      }
    });
  }

  doSaveCusPageSetDtl = (params, callback) => { 
    params = {
      page_uuid: this.page_uuid,
      ...params,
    };
    API.doSaveCusPageSetDtl(params).then(result => {
      if(result.ret === 'success') {
        this.recordId = result.id;
        this.pageCusCurrentId = result.id;
        typeof callback === 'function' && callback(result);
      }
    });
  }

  getSearchTagList = (params, callback) => { 
    params = {
      page_uuid: this.page_uuid,
      tagTitle: this.searchDefineCommonInputVal,
      ...params,
    };
    this.commonConditionTableStore = new TableStore();
    API.getSearchTagList(params).then(result => {
      if(result.api_status) {
        this.commonConditionTableStore.getDatas(result.sessionkey, 1);
        typeof callback === 'function' && callback();
      }
    });
  }

  getSearchTagDtl = (params, callback) => { 
    params = {
      page_uuid: this.page_uuid,
      ...params,
    };
    this.addCommonForm = new WeaForm();
    this.addCommonConditions = [];
    API.getSearchTagDtl(params).then(result => {
      if(result.api_status) {
        this.addCommonForm.initFormFields(result.fields);
        this.addCommonConditions = result.fields;
        this.browserKeyArr = result.browserKeyArr;
        this.editCommonConditionRetdatas = result;
        typeof callback === 'function' && callback();
      }
    });
  }

  //删除搜索页面
  delCusPageSet = (params, callback) => { 
    params = {
      page_uuid: this.page_uuid,
      ...params
    };
    API.delCusPageSet(params).then(result => {
      if(result.api_status) {
        typeof callback === 'function' && callback();
      }
    });
  }

    // 取消隐藏/隐藏
  showAndHideSearchTag = (params, callback) => { 
    params = {
      page_uuid: this.page_uuid,
      ...params
    };
    API.showAndHideSearchTag(params).then(result => {
      if(result.api_status) {
        typeof callback === 'function' && callback();
      }
    });
  }

  // 高级搜索
  getAdConditions = (params, callback) => {
    params = {
      page_uuid: this.page_uuid,
      fromCondition: 1,
      search_type: weesoStore.defalutMoreKey,
      ...params,
    };
    if(!this.topTabsVisible) {
      params = {
        ...params,
        search_type: this.search_type,
      }
    }
    this.adSearchForm = new WeaForm();
    this.adSearchFormConditions = [];
    API.getSearchTagDtl(params).then(result => {
      if(result.api_status) {
        this.adSearchForm.initFormFields(result.fields);
        // this.adSearchForm.resetForm();
        this.adSearchFormConditions = result.fields;
        this.browserKeyArr = result.browserKeyArr;
        typeof callback === 'function' && callback();
      }
    });
  }

  adSearchFormReset = () => {
    this.adSearchForm.resetForm();
    setTimeout(() => {
      this.adSearchForm.updateFields({ CREATEDATE_date_select: '0', BEGINDATE_date_select: '0' });
    }, 0);
  }

  filterParams = () => {
    let browserKeyArr = this.browserKeyArr;
    let adSearchForm = this.adSearchForm;
    const formParams = adSearchForm.getFormParams();
    let search_json = { ...formParams };
    delete search_json.showorder;
    delete search_json.tagName;
    delete search_json.tagTitle;
    browserKeyArr = toJS(browserKeyArr);
    Object.keys(search_json).forEach(key => {
      if(browserKeyArr.some(v => v === key)) {
        if(search_json[key] !== '') {
          search_json[key] = search_json[key].split(',');
        } else {
          delete search_json[key];
        }
      }
    });
    if(Object.keys(search_json).length === 1 && search_json.CREATEDATE_date_select === '0') {
      delete search_json.CREATEDATE_date_select;
    }
    return search_json;
  }
  
}

export default new WeesonSeachSetStore();